package com.xls.future.bmob;

import android.content.Context;
import android.util.Log;

import com.xls.future.FutureApp;
import com.xls.future.entity.GroupData;
import com.xls.future.entity.Photo;
import com.xls.future.utils.AppUtil;
import com.xls.future.utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2017/8/25.
 */

public class BmobUtil {

    private static String TAG = "BmobUtil";

    void queryAllPerson(FindListener<Photo> findListener){
        BmobQuery<Photo> query = new BmobQuery<>();
        query.findObjects(findListener);
    }

    void queryAllPerson(int limit,FindListener<Photo> findListener){
        BmobQuery<Photo> query = new BmobQuery<>();
        query.setLimit(limit);
        query.findObjects(findListener);
    }

    public static void queryGroupCount(Context context){
        BmobQuery<Photo> query = new BmobQuery<>();
        query.groupby(new String[]{"name"});
        query.setHasGroupCount(true);
        boolean isCache = query.hasCachedResult(Photo.class);
        boolean isNetwork = AppUtil.isNetworkAvalible(context);
        LogUtil.d(TAG,"loadPhotosByPage isCache = "+isCache+" | isNetwork="+isNetwork);

        if(isCache && !isNetwork){
            query.setCachePolicy(BmobQuery.CachePolicy.CACHE_ONLY);	// 如果有缓存的话，则先从缓存中获取
        }else{
            query.setCachePolicy(BmobQuery.CachePolicy.NETWORK_ELSE_CACHE);	// 如果没有缓存的话，则先从网络中取
        }
        query.findStatistics(Photo.class, new QueryListener<JSONArray>() {
            @Override
            public void done(JSONArray jsonArray, BmobException e) {
                if(e==null){
                    //查询成功
                    LogUtil.d(TAG,"queryGroupCount :"+jsonArray.toString());
                    if(jsonArray!=null){
                        try {
                            List<GroupData> groupDataList = new ArrayList<>();
                            for(int i=0;i<jsonArray.length();i++){
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                int count = jsonObject.optInt("_count");
                                String name = jsonObject.optString("name");
                                groupDataList.add(new GroupData(count,name));
                            }
                            FutureApp.getInst().setGroups(groupDataList);
                        }catch (JSONException e1){
                            LogUtil.e(TAG,e1.getMessage());
                        }

                    }
                }else{
                    LogUtil.e(TAG,e.getMessage());
                }
            }
        });
    }

    public static void recordErrorCount(String imageUrl){
        BmobQuery<Photo> query = new BmobQuery<>();
        query.addWhereEqualTo("imgUrl",imageUrl);
        query.findObjects(new FindListener<Photo>() {
            @Override
            public void done(List<Photo> list, BmobException e) {
                if(list==null || list.size()<=0){
                    return;
                }
                final Photo photo = list.get(0);
                LogUtil.d(TAG,"根据URL查询Bean成功:"+photo.toString());
                photo.increment("errorCount");
                photo.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        LogUtil.w(TAG,"更新 "+photo.getImgUrl()+" 失败次数 +1");
                    }
                });
            }
        });
    }
}
